<?php
/**
 * 集阅读模块微站定义
 *
 * @author 别具一格
 * @url http://bbs.we7.cc/
 */
defined('IN_IA') or exit('Access Denied');

class Tim_albumModuleSite extends WeModuleSite {
	public $table_album = 'tim_album';
	public function doWebList() {
		global $_GPC, $_W;
		$uniacid = $_W['uniacid'];
		$pindex = max(1, intval($_GPC['page']));
		$psize = 20;
		$list = pdo_fetchall("SELECT * FROM ".tablename($this->table_album)." WHERE uniacid = '{$_W['uniacid']}' ORDER BY album_id DESC LIMIT " . ($pindex - 1) * $psize .',' .$psize);
		$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_album) . " WHERE uniacid = '{$_W['uniacid']}'");
		$pager = pagination($total, $pindex, $psize);
		include $this->template('list');
	}

	public function doMobileCover1() { 
		include $this->template('index');
	}

	public function doWebUser() {
		global $_GPC, $_W;
		$uniacid = $_W['uniacid'];
		load()->func('tpl');
		$album_id = intval($_GPC['album_id']);
		$album_name = pdo_fetch("SELECT * FROM ".tablename('tim_album')." WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}'");
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if('post' == $op){//添加或修改
			$user_id = intval($_GPC['user_id']);
			if(!empty($user_id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('tim_album_user')." where album_id =$album_id and user_id=$user_id");
			empty($item)?message('亲,数据不存在！', '', 'error'):"";	
			}
			
			if(checksubmit('submit')){
				empty ($_GPC['user_name'])?message('亲,用户名不能为空'):$user_name=$_GPC['user_name'];
			$password =$_GPC['password'];
			$data = array(
				'user_id'=>$user_id,
				'uniacid'=>$uniacid,
				'user_name' =>$user_name,
				'password'=>$password,
				'album_id'=>$album_id
			);
				if(empty($user_id)){
						pdo_insert('tim_album_user', $data);//添加数据
						message('数据添加成功！', $this->createWebUrl('list', array('op' => 'display')), 'success');
				}else{
						pdo_update('tim_album_user', $data, array('user_id' => $user_id));
						message('数据更新成功！', $this->createWebUrl('list', array('op' => 'display')), 'success');
				}
				
			}else{
				include $this->template('user');
			}
			
			
		}else if('del' == $op){//删除
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('tim_album_user')."  where user_id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$user_id = intval($_GPC['user_id']);
			$row = pdo_fetch("SELECT user_id FROM ".tablename('tim_album_user')." WHERE user_id = :user_id", array(':user_id' => $user_id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('list', array('op' => 'display')), 'error');
			}
			pdo_delete('tim_album_user', array('user_id' => $user_id));
			message('删除成功！', referer(), 'success');
			
		}else if('display' == $op){//显示
			$album_id = intval($_GPC['album_id']);
			$album_name = pdo_fetch("SELECT * FROM ".tablename('tim_album')." WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}'");
			$pindex = max(1, intval($_GPC['page']));
			$psize = 20;
			$list = pdo_fetchall("SELECT * FROM ".tablename('tim_album_user')." WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}' ".$condition." ORDER BY user_id DESC LIMIT " . ($pindex - 1) * $psize .',' .$psize);
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_album_user') . " WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}' ".$condition);
			$pager = pagination($total, $pindex, $psize);
			include $this->template('user');
		}

	}

	public function doWebLogs() {
		global $_GPC, $_W;
		$uniacid = $_W['uniacid'];
		load()->func('tpl');
		$album_id = intval($_GPC['album_id']);
		$album_name = pdo_fetch("SELECT * FROM ".tablename('tim_album')." WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}'");
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		if('post' == $op){//添加或修改
			$pic_id = intval($_GPC['pic_id']);
			if(!empty($pic_id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('tim_album_pic')." where album_id =$album_id and pic_id=$pic_id");
			empty($item)?message('亲,数据不存在！', '', 'error'):"";	
			}
			
			if(checksubmit('submit')){
				empty ($_GPC['pic'])?message('亲,图片不能为空'):$pic=$_GPC['pic'];
			$pic_name =$_GPC['pic_name'];
			$data = array(
				'pic_id'=>$pic_id,
				'uniacid'=>$uniacid,
				'pic' =>$pic,
				'pic_name'=>$pic_name,
				'album_id'=>$album_id
			);
				if(empty($pic_id)){
						pdo_insert('tim_album_pic', $data);//添加数据
						message('数据添加成功！', $this->createWebUrl('list', array('op' => 'display')), 'success');
				}else{
						pdo_update('tim_album_pic', $data, array('pic_id' => $pic_id));
						message('数据更新成功！', $this->createWebUrl('list', array('op' => 'display')), 'success');
				}
				
			}else{
				include $this->template('logs');
			}
			
			
		}else if('del' == $op){//删除
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('tim_album_pic')."  where pic_id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$pic_id = intval($_GPC['pic_id']);
			$row = pdo_fetch("SELECT pic_id FROM ".tablename('tim_album_pic')." WHERE pic_id = :pic_id", array(':pic_id' => $pic_id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('list', array('op' => 'display')), 'error');
			}
			pdo_delete('tim_album_pic', array('pic_id' => $pic_id));
			message('删除成功！', referer(), 'success');
			
		}else if('display' == $op){//显示
			$album_id = intval($_GPC['album_id']);
			$album_name = pdo_fetch("SELECT * FROM ".tablename('tim_album')." WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}'");
			$pindex = max(1, intval($_GPC['page']));
			$psize = 20;
			$list = pdo_fetchall("SELECT * FROM ".tablename('tim_album_pic')." WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}' ".$condition." ORDER BY pic_id DESC LIMIT " . ($pindex - 1) * $psize .',' .$psize);
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_album_pic') . " WHERE uniacid = '{$_W['uniacid']}' AND album_id = '{$album_id}' ".$condition);
			$pager = pagination($total, $pindex, $psize);
			include $this->template('logs');
		}

	}


	public function doMobileMain() {
		global $_W,$_GPC;
		load()->func('tpl');
		$uniacid = $_W['uniacid'];
		$album_id = intval($_GPC['album_id']);
		$reply = pdo_fetchall("SELECT * FROM ". tablename('tim_album_pic') ." WHERE album_id = :album_id AND uniacid = :uniacid ORDER BY `pic_id` ASC", array(':album_id' => $album_id, 'uniacid' => $uniacid));
		$users = pdo_fetchall("SELECT user_name,password FROM ". tablename('tim_album_user') ." WHERE album_id = :album_id AND uniacid = :uniacid ORDER BY `user_id` DESC", array(':album_id' => $album_id, 'uniacid' => $uniacid));
		$pic_info = pdo_fetch("SELECT * FROM ". tablename('tim_album') ." WHERE album_id = :album_id AND uniacid = :uniacid", array(':album_id' => $album_id, 'uniacid' => $uniacid));
		$pic_id = intval($_GPC['pic_id']);
		if(!empty($pic_id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('tim_album_pic')." where album_id =$album_id and pic_id=$pic_id");
		}
		if (!empty($reply)) {
			
				include $this->template('index');
		}
	}

	public function doMobileAddpic() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$album_id = intval($_GPC['album_id']);
		empty ($_GPC['pic'])?message('亲,图片不能为空'):$pic=$_GPC['pic'];
		$pic_name =$_GPC['pic_name'];
		$data = array(
			'uniacid'=>$uniacid,
			'pic' =>$pic,
			'pic_name'=>$pic_name,
			'album_id'=>$album_id
		);
		pdo_insert('tim_album_pic', $data);//添加数据
		include $this->template('index');
	

	}

}